package com.bootdo.sso;

import com.alibaba.fastjson.JSONObject;
import com.bootdo.common.config.ApplicationContextRegister;
import com.bootdo.system.dao.UserDao;
import com.bootdo.system.domain.UserDO;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.subject.Subject;
import org.springframework.core.Ordered;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;



public class LoadUserInfoImp extends CheckTokenService   {



    @Override
    public void loadLocalUserAuthentication(String json) {
        JSONObject jsonObject = (JSONObject) JSONObject.parse(json);
        Subject subject = SecurityUtils.getSubject();
        if(jsonObject.containsKey("user_name")){
            Map<String, Object> map = new HashMap<>(16);
            map.put("username", jsonObject.getString("user_name"));
            UserDao userMapper = ApplicationContextRegister.getBean(UserDao.class);
            // 查询用户信息
            UserDO user = userMapper.list(map).get(0);
            if(null != user){
                UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
                subject.login(token);
                return;
            }
        }
    }
    @Override
    public void loadLocalUserAuthentication(UserDO user) {
        if(null != user){
            UsernamePasswordToken token = new UsernamePasswordToken(user.getUsername(), user.getPassword());
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);
            return;
        }
    }



}
